package com.xx.doublexchat.common.chat.event.listener;

import com.xx.doublexchat.common.chat.domain.dto.MsgSendMessageDTO;
import com.xx.doublexchat.common.chat.event.MessageSendEvent;
import constant.MQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;
import service.MQProducer;

@Slf4j
@Component
public class MessageSendListener {
    /**
     * rocketMq
     */
    @Autowired
    private MQProducer mqProducer;

    /**
     * 事件监听----发送消息----通过MQ推送消息-----事务执行前发送
     * @param event
     */
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = MessageSendEvent.class, fallbackExecution = true)
    public void messageRoute(MessageSendEvent event) {
        Long msgId = event.getMsgId();
        mqProducer.sendSecureMsg(MQConstant.SEND_MSG_TOPIC, new MsgSendMessageDTO(msgId), msgId);
    }
}
